/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2007 Miranda ICQ/IM project, 
all portions of this codebase are copyrighted to the people 
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or ( at your option ) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
aLONG with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
#include "commonheaders.h"
#include "ctrl.h"
#include "svcTimezone.h"

namespace NCtrl 
{
	namespace TzCombo 
	{
		struct CTzCtrl : public CTRL
		{
			INT	curSel;
		};

		/**
		 * This functions fills a combobox given by @hCtrl with
		 * all items of the global timezone manager
		 *
		 * @param		hCtrl	-	HWND of the combobox
		 *
		 * @return	nothing
		 **/
		VOID Init( HWND hCtrl )
		{
			INT			i, c;
			CTimeZone	*ptz;
			CTzCtrl		*ctrl;
			
			ctrl = new CTzCtrl();
			if( ctrl )
			{
				SetUserData( hCtrl, ctrl );
				
				ctrl->nType = 10;
				ctrl->wFlags = 0;
				for( i = 0, c = NServices::TimeZones::Manager.Bias.getCount(); i < c; i ++ )
				{
					ptz = NServices::TimeZones::Manager.Bias[i];
					if( ptz )
					{
						ComboBox_AddItem( hCtrl, ptz->pszDisplay, ( LPARAM ) ptz );
					}
				}
				ctrl->curSel = ComboBox_AddString( hCtrl, TranslateT("Unspecified") );
				ComboBox_SetCurSel( hCtrl, ctrl->curSel );
			}
		}
		
		/**
		 * This functions removes the user data from a combobox.
		 *
		 * @param		hCtrl	-	HWND of the combobox
		 *
		 * @return	nothing
		 **/
		VOID Destroy( HWND hCtrl )
		{
			CTzCtrl * ctrl = ( CTzCtrl* ) GetUserData( hCtrl );

			if( PtrIsValid( ctrl ) )
			{
				SetUserData( hCtrl, NULL );
				delete ctrl;
			}
		}
		
		/**
		 * This functions selects the combobox item which matches the contact's timezone.
		 *
		 * @param		hCtrl		- HWND of the combobox
		 * @param		hContact	- HANDLE of the contact whose timezone to select
		 * @param		szProto		- the contact's protocol
		 *
		 * @return	nothing
		 **/
		VOID Select( HWND hCtrl, HANDLE hContact, LPCSTR szProto )
		{
			CTzCtrl * ctrl = ( CTzCtrl* ) GetUserData( hCtrl );

			if( PtrIsValid( ctrl ) && !(ctrl->wFlags & CTRLF_CHANGED ) )
			{
				DWORD dwIndex;
				LPTSTR pszName; 

				ctrl->wFlags = NServices::TimeZones::Manager.GetContactTimeZoneIndexCtrl( hContact, szProto, dwIndex, pszName );
				if( ctrl->wFlags )
				{
					CTimeZone * ptz;
					INT nItemIndex = 0;

					while( PtrIsValid( ptz = ( CTimeZone * )ComboBox_GetItemData( hCtrl, nItemIndex ) ) ) 
					{
						if (ptz->pszName && pszName){
							if( mir_tcscmp(ptz->pszName, pszName) == 0 /*|| ptz->dwIndex == dwIndex*/) {
								ComboBox_SetCurSel( hCtrl, nItemIndex );
								ctrl->curSel = ComboBox_GetCurSel( hCtrl );
								return;
							}
						}
						else if ( ptz->dwIndex && dwIndex ){
							if( ptz->dwIndex == dwIndex) {
								ComboBox_SetCurSel( hCtrl, nItemIndex );
								ctrl->curSel = ComboBox_GetCurSel( hCtrl );
								if (!pszName) {
									DB::Setting::Delete( hContact, USERINFO, "TzIndex" );
									DB::Setting::WriteAString( hContact, USERINFO, "TzName", (LPSTR) mir_t2a(ptz->pszName) );
								}
								return;
							}
						}
						nItemIndex++;
					}	
				}
			}
		}

		/**
		 * This function writes the combobox's item as the contact's timezone.
		 *
		 * @param		hCtrl		- HWND of the combobox
		 * @param		hContact	- HANDLE of the contact whose timezone to select
		 * @param		szProto		- the contact's protocol
		 *
		 * @return	nothing
		 **/
		VOID Write( HWND hCtrl, HANDLE hContact, LPCSTR szProto )
		{
			CTzCtrl * ctrl = ( CTzCtrl* ) GetUserData( hCtrl );

			if( PtrIsValid( ctrl ) && (ctrl->wFlags & CTRLF_CHANGED ) )
			{
				const CTimeZone * ptz = ( CTimeZone * )ComboBox_GetItemData( hCtrl, ComboBox_GetCurSel( hCtrl ) );

				if( PtrIsValid( ptz ) )
				{
					DB::Setting::WriteAString	( hContact, USERINFO, "TzName", (LPSTR) mir_t2a(ptz->pszName) );
					if( !hContact )
					{
						DB::Setting::WriteByte( szProto, SET_CONTACT_TIMEZONE, ptz->ToMirandaTimezone() );
						// DB::Setting::WriteDWord(szProto, "TzIndex", ptz->dwIndex );
						ctrl->wFlags |= CTRLF_HASPROTO;
					}
					else
					{
						// used by clist plugins and tabsrmm
						DB::Setting::WriteByte( hContact, szProto, SET_CONTACT_TIMEZONE, ptz->ToMirandaTimezone() );
						DB::Setting::WriteByte( hContact, USERINFO, SET_CONTACT_TIMEZONE, ptz->ToMirandaTimezone() );
						ctrl->wFlags |= CTRLF_HASCUSTOM;
					}
					ctrl->wFlags &= ~CTRLF_CHANGED;
				}
				else
				{
					DB::Setting::Delete( hContact, USERINFO, "TzIndex" );
					DB::Setting::Delete( hContact, USERINFO, "TzName" );
					DB::Setting::Delete( hContact, USERINFO, SET_CONTACT_TIMEZONE );
					DB::Setting::Delete( hContact, szProto, SET_CONTACT_TIMEZONE );
					ctrl->wFlags &= ~( CTRLF_HASCUSTOM | CTRLF_CHANGED );

					Select( hCtrl, hContact, szProto );
				}
				InvalidateRect( hCtrl, NULL, TRUE );
			}
		}

		/**
		 * The user changed combobox selection, so mark it changed.
		 *
		 * @param		hCtrl			-	HWND of the combobox
		 *
		 * @return	nothing
		 **/
		VOID Selchange( HWND hCtrl )
		{
			CTzCtrl * ctrl = ( CTzCtrl* ) GetUserData( hCtrl );

			if( PtrIsValid( ctrl ) && !( ctrl->wFlags & CTRLF_CHANGED ) && ctrl->curSel != ComboBox_GetCurSel( hCtrl ) )
			{
				ctrl->wFlags |= CTRLF_CHANGED;
				SendMessage( GetParent( GetParent( hCtrl ) ), PSM_CHANGED, 0, 0 );
			}
		}

		/**
		 * This function fills @szTime with the current time
		 * according to the combobox's selected timezone.
		 *
		 * @param		hCtrl		- HWND of the combobox
		 * @param		szTime		- string to fill with the current time
		 * @param		cchTime		- number of characters the string can take
		 *
		 * @return	nothing
		 **/
		VOID GetTime( HWND hCtrl, LPTSTR szTime, WORD cchTime )
		{
			const CTimeZone * ptz = ( CTimeZone * )ComboBox_GetItemData( hCtrl, ComboBox_GetCurSel( hCtrl ) );

			if( PtrIsValid( ptz ) )
			{
				MTime now;
				TIME_ZONE_INFORMATION tzi = *ptz;

				now.GetTimeUTC();
				now.UTCToTzSpecificLocal( &tzi );

				now.TimeFormat( szTime, cchTime );
			}
			else
			{
				_tcscpy( szTime, _T( "--:--" ) );
			}
		}

	} /* namespace TzCombo */
} /* namespace NCtrl */
		